package com.lx.wxdemo.controller;

import com.lx.wxdemo.service.WxService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description TODO
 * @Author lx
 * @Date 2021/1/4 15:39
 **/
@RestController
public class WxController {

    @Autowired
    private WxService wxService;

    @RequestMapping("/wxdemo/login")
    public void wxLogin(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("进入微信小程序后台=========");

        //1、成功进入Java后端，进行校验及处理
        //接收小程序传过来的account和password
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        // User user = new User(account,password);

        //判断是否登陆成功
        Boolean isLogin = wxService.login(account, password);
        Map<String, String> map = new HashMap<String, String>();
        if (isLogin ==true) {
            map.put("message", "ok");
        } else {
            map.put("message", "err");
        }
        //要将Map转化为JSON，才可以传数据返回小程序
        JSONObject mapObject = JSONObject.fromObject(map);
        System.out.println("mapObject:" + mapObject.toString());
        //从服务器传mapObject数据到小程序
        try {
            response.getWriter().write(mapObject.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
